package com.future.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.future.mapper.DrivingDriverMapper;
import com.future.mapper.DrivingStatisMapper;
import com.future.po.DrivingDriver;
import com.future.po.DrivingStatis;
import com.future.service.DrivingStatisService;
import com.future.utils.BeanUtils;
import com.future.utils.DateUtil;
import com.future.vo.ChartVo;
import com.future.vo.DrivingStatisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DrivingStatisServiceImpl extends ServiceImpl<DrivingStatisMapper, DrivingStatis> implements DrivingStatisService {


    @Override
    public ChartVo statisToWeekBehave(Integer id) {
        ChartVo chartVo = new ChartVo();
        List<String> days = DateUtil.getSevenDate(14);
        chartVo.setDays(days);
        LambdaQueryWrapper<DrivingStatis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrivingStatis::getUserId,id);
        wrapper.between(DrivingStatis::getDate,days.get(0),days.get(days.size()-1));
        wrapper.orderByAsc(DrivingStatis::getCreateTime);
        List<DrivingStatis> drivingStatisList = baseMapper.selectList(wrapper);
        ArrayList<DrivingStatisVo> drivingStatisVos = new ArrayList<>();
        for (DrivingStatis drivingStatis: drivingStatisList) {
            DrivingStatisVo drivingStatisVo = new DrivingStatisVo();
            BeanUtils.copyProperties(drivingStatis, drivingStatisVo);

            drivingStatisVos.add(drivingStatisVo);
        }
        chartVo.setOption(drivingStatisVos);
        return chartVo;
    }
}
